package com.aos.erp.common.config;

import com.aos.erp.common.web.AuthorizationContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;

/**
 * @program: erp
 * @description:
 * @author: tanzhiyu
 * @create: 2018-11-21 19:49
 **/

@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 生成远程调用认证token
        //String token = TokenUtil.TokenCreate("feign");
        // 放到redis,设置时长为10S，一般10S后还没有完成请求则token失效
        //redisProxy.setex(token, 10, token);
        //设置token，关键方法
        String url = requestTemplate.url();
        if(!url.equals("/user/login") && !url.equals("/sync/synOrgInfo")){
            requestTemplate.header("Authorization",   AuthorizationContext.get());
        }
    }

    /*@Bean
    public RequestInterceptor headerInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
               ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                        .getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();
                Enumeration<String> headerNames = request.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        String values = request.getHeader(name);
                        requestTemplate.header(name, values);
                    }
                }
                requestTemplate.header("token", TokenContext.get());
            }
        };
    }*/

}
